package models

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"github.com/astaxie/beego"
	"net/url"
)

func RegisterDB() {

	dbhost := beego.AppConfig.String("db.host")
	dbport := beego.AppConfig.String("db.port")
	dbname := beego.AppConfig.String("db.name")
	dbuser := beego.AppConfig.String("db.user")
	dbpass := beego.AppConfig.String("db.pass")
	timezone := beego.AppConfig.String("db.timezone")

	if dbport == "" {
		dbport = "3306"
	}

	dsn := dbuser + ":" + dbpass + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8"
	if timezone != "" {

		dsn = dsn + "&loc=" + url.QueryEscape(timezone)
	}

	orm.RegisterDataBase("default", "mysql", dsn, 5, 30)
	if beego.AppConfig.String("runmode") == "dev" {
		orm.Debug = true
	}

	orm.RegisterModel(new(User))
	orm.RegisterModel(new(Shop))
	orm.RegisterModel(new(Category))
	orm.RegisterModel(new(Goods))
	orm.RegisterModel(new(Image))
	orm.RegisterModel(new(Role))
	orm.RegisterModel(new(Table))
	orm.RegisterModel(new(Ring))
	orm.RegisterModel(new(RingCate))
	orm.RegisterModel(new(GoodsImage))
	orm.RegisterModel(new(ShopImage))
	orm.RegisterModel(new(UserToken))
}

type UserToken struct {
	Id            int64 `json:"id" pk`
	UserId        int64 `json:user_id`
	Token         string `json:"token"`
	LastLoginTime int64 `json:"last_login_time"`
}

type Role struct {
	Id     int64 `form:"id" pk`
	Title  string `form:"title"`
	Name   string `form:"name"`
	Remark string `form:"remark"`
	Status string `form:"status"`
}

type User struct {
	Id            int64 `form:"-" pk json:id`
	UserName      string  `form:"UserName" json:userName`
	PassWord      string `form:"-" json:passWord`
	NickName      string `form:"-" json:nickName`
	Email         string `form:"Email" json:email`
	Remark        string `form:"Remark" json:remark`
	Status        int8 `form:"-" json:status`
	LastLoginTime int64 `form:"-" json:lastLoginTime`
	CreateTime    int64 `form:"-" json:createTime`
	Phone         string `form:"Phone" json:phone`
	Address       string `form:"Address" json:address`
	Image         string `form:"Image" json:image`
	Birth         string  `form:"Birth" json:birth`
	Role          *Role `orm:"rel(one)" json:role`
	BaiduToken    string ` json:baiduToken`
	Token         string ` json:token`
}

type Shop struct {
	Id         int64 `form:"-" pk`
	ShopName   string  `form:"shop_name"`
	Tel        string `form:"tel"`
	Address    string `form:"address"`
	CreateTime int64 `form:"create_time"`
	UserId     int64 `form:"user_id"`
	Code       string `form:"code"`
	ImageList  []ShopImage `orm:"-"`
}

type Category struct {
	Id       int64 `form:"id" pk`
	Name     string  `form:"name"`
	ParentId int32 `form:"parent_id"`
	ShopId   int32 `form:"shop_id"`
}

type Goods struct {
	Id            int64 `form:"id" pk`
	Name          string  `form:"name"`
	OriginalPrice int32 `form:"original_price"`
	Price         int32 `form:"price"`
	Stock         int32 `form:"stock"`
	ShopId        int32 `form:"shop_id"`
	CategoryId    int32 `form:"category_id"`
	Image         string `form:"image"`
	Infomation    string `form:"infomation"`
	ImageList     []GoodsImage `orm:"-"`
}

type Image struct {
	Id   int64 `form:"id" pk`
	Path string  `form:"path"`
	Type int8    `form:"type"`
	//GoodId int64   `form:"goods_id"`
}

type GoodsImage struct {
	Id     int64 `form:"id" pk`
	Path   string  `form:"path"`
	Type   int8    `form:"type"`
	GoodId int64   `form:"goods_id"`
}
type ShopImage struct {
	Id     int64 `form:"id" pk`
	Path   string  `form:"path"`
	Type   int8    `form:"type"`
	ShopId int64   `form:"shop_id"`
}

type File struct {
	Id       int64 `form:"-"`
	FilePath string  `form:"-"`
	FileName string `form:"-"`
	FileSize int64  `form:"-"`
}

type Table struct {
	Id         int64 `form:"-"`
	ShopId     int64 `form:"shop_id" `
	TableId    int64 `form:"table_id" `
	TableName  string `form:"table_name`
	CreateTime int64 `form:"create_time`
	TableSize  int64`form:"table_size`
	Status     int`form:"status`
	QRcode     string`form:"qr_code`
}


